package com.ktgj.customer.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateTime;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.helper.StringHelper;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.Insurance.hlModel.Insurance;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.IClaimDelayinsuranceService;
import com.ktgj.customer.service.ICustomerService;
import com.ktgj.customer.service.ILoginService;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.AirportApi;
import com.ktgj.train.api.RemoteTrainService;
import com.ktgj.train.api.domain.CityVoApi;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.domain.ClaimDelayinsurance;
import com.ktgj.customer.service.IClaimDelayinsuranceService;

import javax.servlet.http.HttpServletRequest;

import static com.ktgj.common.core.constant.CustomerConstants.customer;

/**
 * 保险理赔合并
Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-17
 */
@Service
public class ClaimDelayinsuranceServiceImpl implements IClaimDelayinsuranceService
{
    @Autowired
    private ClaimDelayinsuranceMapper claimDelayinsuranceMapper;

    @Autowired
    private CaseSysMapper caseSysMapper;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private InsuranceRecordMapper insuranceRecordMapper;

    @Autowired
    private GuaranteeMapper guaranteeMapper;

    @Autowired
    private RemoteTrainService remoteTrainService;

    @Autowired
    private InsuranceTypeMapper insuranceTypeMapper;

    @Autowired
    private ClaimDelayinsuranceImageMapper claimDelayinsuranceImageMapper;

    @Autowired
    private RemoteFlightService remoteFlightService;

    @Autowired
    private CaseTypeMapper caseTypeMapper;

    @Autowired
    private CaseSubTypeMapper caseSubTypeMapper;

    @Autowired
    private CaseClaimMapper caseClaimMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private ILoginService loginService;

    /**
     * 查询保险理赔合并

     * 
     * @param delayinsuranceId 保险理赔合并
ID
     * @return 保险理赔合并

     */
    @Override
    public ClaimDelayinsurance selectClaimDelayinsuranceById(Long delayinsuranceId)
    {
        return claimDelayinsuranceMapper.selectClaimDelayinsuranceById(delayinsuranceId);
    }

    /**
     * 查询保险理赔合并
列表
     * 
     * @param claimDelayinsurance 保险理赔合并

     * @return 保险理赔合并

     */
    @Override
    public List<ClaimDelayinsurance> selectClaimDelayinsuranceList(ClaimDelayinsurance claimDelayinsurance)
    {
        return claimDelayinsuranceMapper.selectClaimDelayinsuranceList(claimDelayinsurance);
    }

    /**
     * 新增保险理赔合并

     * 
     * @param claimDelayinsurance 保险理赔合并

     * @return 结果
     */
    @Override
    public int insertClaimDelayinsurance(ClaimDelayinsurance claimDelayinsurance)
    {
        return claimDelayinsuranceMapper.insertClaimDelayinsurance(claimDelayinsurance);
    }

    /**
     * 修改保险理赔合并

     * 
     * @param claimDelayinsurance 保险理赔合并

     * @return 结果
     */
    @Override
    public int updateClaimDelayinsurance(ClaimDelayinsurance claimDelayinsurance) throws Exception {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        claimDelayinsurance.setCreateTime(date);
        if (claimDelayinsurance.getDeparture() != null){
            CityVoApi cityVoApi = new CityVoApi();
            cityVoApi.setCityName(claimDelayinsurance.getDeparture());
            cityVoApi = remoteTrainService.cityVo(cityVoApi);
            if (cityVoApi != null){
                claimDelayinsurance.setDeparturecode(cityVoApi.getCityCode());
            }
        }
        if (claimDelayinsurance.getArrival() != null){
            CityVoApi cityVoApi = new CityVoApi();
            cityVoApi.setCityName(claimDelayinsurance.getDeparture());
            cityVoApi = remoteTrainService.cityVo(cityVoApi);
            if (cityVoApi != null){
                claimDelayinsurance.setArrivalcode(cityVoApi.getCityCode());
            }
        }

        if (claimDelayinsurance.getWorkOrder() != null && claimDelayinsurance.getInvoiceId() != null){
            CaseSys caseSys = new CaseSys();
            caseSys.setCaseId(claimDelayinsurance.getInvoiceId());
            caseSys.setCaseSubject(claimDelayinsurance.getWorkOrder().getTitle());
            int i = caseSysMapper.updateCaseSys(caseSys);
            if (i <= 0){
                throw new Exception("工单信息修改失败");
            }
        }

        if (claimDelayinsurance.getFlight() != null){
            claimDelayinsurance.setFlightno(claimDelayinsurance.getFlight().getFlightNum());
            claimDelayinsurance.setDepartureTime(format.parse(claimDelayinsurance.getFlight().getFlightDate()));
            claimDelayinsurance.setFlightstatus(Long.valueOf(claimDelayinsurance.getFlight().getFlightStart()));
        }

        return claimDelayinsuranceMapper.updateClaimDelayinsurance(claimDelayinsurance);
    }

    /**
     * 批量删除保险理赔合并

     * 
     * @param delayinsuranceIds 需要删除的保险理赔合并
ID
     * @return 结果
     */
    @Override
    public int deleteClaimDelayinsuranceByIds(Long[] delayinsuranceIds)
    {
        return claimDelayinsuranceMapper.deleteClaimDelayinsuranceByIds(delayinsuranceIds);
    }

    /**
     * 删除保险理赔合并
信息
     * 
     * @param delayinsuranceId 保险理赔合并
ID
     * @return 结果
     */
    @Override
    public int deleteClaimDelayinsuranceById(Long delayinsuranceId)
    {
        return claimDelayinsuranceMapper.deleteClaimDelayinsuranceById(delayinsuranceId);
    }

    /**
     * 查询保险理赔
     * */
    @Override
    public List<ClaimDelayinsurance> claimDelayinsuranceList(ClaimDelayinsuranceVo claimDelayinsuranceVo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ClaimDelayinsurance> claimDelayinsuranceList = claimDelayinsuranceMapper.claimDelayinsuranceList(claimDelayinsuranceVo);
        if (claimDelayinsuranceList != null){
            for (ClaimDelayinsurance claimDelayinsurance : claimDelayinsuranceList) {
                if (claimDelayinsurance.getOrderId()!=null)
                {
                    claimDelayinsurance.setOrderNum(claimDelayinsurance.getOrderId().toString());
                }


                /** 工单信息 */
                if (claimDelayinsurance.getInvoiceId() != null){
                    CaseSys caseSys = caseSysMapper.selectCaseSysById(claimDelayinsurance.getInvoiceId());
                    if (caseSys != null){
                        WorkOrder workOrder = new WorkOrder();
                        workOrder.setDelay(caseSys.getCaseId().toString());
                        workOrder.setTitle(caseSys.getCaseSubject());
                        claimDelayinsurance.setWorkOrder(workOrder);
                    }
                }

                /** 会员信息 */
                if (claimDelayinsurance.getCustomerId() != null){
                    CustomerIdVo customerIdVo = new CustomerIdVo();
                    customerIdVo.setCustomerId(Long.valueOf(claimDelayinsurance.getCustomerId()));
                    try {
                        customerIdVo = customerService.customerId(customerIdVo);
                    } catch (Exception e){
                        throw new Exception(e.getMessage());
                    }
                    if (customerIdVo != null){
                        Member member = new Member();
                        member.setCardNum(customerIdVo.getCustomerCard());
                        member.setName(customerIdVo.getCustomerName());
                        member.setPhone(customerIdVo.getCustomerPhone());
                        claimDelayinsurance.setMember(member);
                    }
                }

                /** 保险信息 */
                if (claimDelayinsurance.getDelayclaimorderid() != null){
                    Insurance insurance = new Insurance();
                    Guarantee guarantee = guaranteeMapper.selectGuaranteeById(claimDelayinsurance.getDelayclaimorderid());
                    if (guarantee != null){
                        insurance.setPolicyNum(guarantee.getGuaranteeNo());
                        if (guarantee.getInsTypeId() != null){
                            InsuranceType insuranceType = insuranceTypeMapper.selectInsuranceType(guarantee.getInsTypeId());
                            if (insuranceType != null){
                                insurance.setPolicyType(insuranceType.getInsTypeName());
                            }
                        }
                    }
                    claimDelayinsurance.setInsurance(insurance);
                }

                /** 航班信息 */
                Flight flight = new Flight();
                flight.setFlightNum(claimDelayinsurance.getFlightno());

                if (claimDelayinsurance.getDeparturecode() != null && claimDelayinsurance.getArrivalcode() != null){
                    flight.setVoyage(claimDelayinsurance.getDeparturecode() + "-" + claimDelayinsurance.getArrivalcode());
                }
                if (claimDelayinsurance.getDepartureTime() != null){
                    flight.setFlightDate(format.format(claimDelayinsurance.getDepartureTime()));
                }

                flight.setFlightStart(claimDelayinsurance.getFlightstatus().toString());

                claimDelayinsurance.setFlight(flight);

                if (claimDelayinsurance.getCreateTime() != null){
                    claimDelayinsurance.setCreateDate(format.format(claimDelayinsurance.getCreateTime()));
                }

                if (claimDelayinsurance.getState() != null){
                    if (claimDelayinsurance.getState() == 0){
                        claimDelayinsurance.setStart("初始");
                    }
                    else if (claimDelayinsurance.getState() == 1){
                        claimDelayinsurance.setStart("理赔成功");
                    }
                    else if (claimDelayinsurance.getState() == 2){
                        claimDelayinsurance.setStart("理赔失败");
                    }
                    else if (claimDelayinsurance.getState() == 3){
                        claimDelayinsurance.setStart("安心打款失败");
                    }
                    else if (claimDelayinsurance.getState() == 4){
                        claimDelayinsurance.setStart("安心打款成功");
                    }
                }
            }
        }
        return claimDelayinsuranceList;
    }

    /**
     * 查询待修改的保险理赔信息
     * */
    @Override
    public UpdateClaimDelayinsurance selClaimDelayinsurance(Long id) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        UpdateClaimDelayinsurance updateClaimDelayinsurance = new UpdateClaimDelayinsurance();
        ClaimDelayinsurance claimDelayinsurance = claimDelayinsuranceMapper.selClaimDelayinsurance(id);
        if (claimDelayinsurance != null){
            updateClaimDelayinsurance.setId(claimDelayinsurance.getDelayinsuranceId());

            CustomerIdVo customerIdVo = new CustomerIdVo();
            customerIdVo.setCustomerId(Long.valueOf(claimDelayinsurance.getCustomerId()));
            try {
                customerIdVo = customerService.customerId(customerIdVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }

            if (customerIdVo != null){
                updateClaimDelayinsurance.setCardType(customerIdVo.getCustomerCardType());
                updateClaimDelayinsurance.setCardNum(customerIdVo.getCustomerCard());
                updateClaimDelayinsurance.setCustomerName(customerIdVo.getCustomerName());
                updateClaimDelayinsurance.setPhone(customerIdVo.getCustomerPhone());
            }

            updateClaimDelayinsurance.setFlightNum(claimDelayinsurance.getFlightno());

            if (claimDelayinsurance.getDeparture() != null && claimDelayinsurance.getArrival() != null){
                updateClaimDelayinsurance.setVoyage(claimDelayinsurance.getDeparture() + "-" + claimDelayinsurance.getArrival());
            }

            if (claimDelayinsurance.getDepartureTime() != null){
                updateClaimDelayinsurance.setFlightTime(format.format(claimDelayinsurance.getDepartureTime()));
            }

            if (claimDelayinsurance.getDelaytime() != null){
                updateClaimDelayinsurance.setDelayTime(format.format(claimDelayinsurance.getDelaytime()));
            }

            if (claimDelayinsurance.getDepartureTime() != null){
                updateClaimDelayinsurance.setOriginalSetOffTime(format.format(claimDelayinsurance.getDepartureTime()));
            }

            if (claimDelayinsurance.getArriveDate() != null){
                updateClaimDelayinsurance.setOriginalFinishTime(format.format(claimDelayinsurance.getArriveDate()));
            }

            if (claimDelayinsurance.getRealFlyawayDate() != null){
                updateClaimDelayinsurance.setActualSetOffTime(format.format(claimDelayinsurance.getRealFlyawayDate()));
            }

            if (claimDelayinsurance.getRealArriveDate() != null){
                updateClaimDelayinsurance.setActualFinishTime(format.format(claimDelayinsurance.getRealArriveDate()));
            }

            if (claimDelayinsurance.getFlightstatus() != null){
                updateClaimDelayinsurance.setFlightStart(claimDelayinsurance.getFlightstatus().toString());
            }

            updateClaimDelayinsurance.setClaimPolicyNum(claimDelayinsurance.getInsno());

            if (claimDelayinsurance.getOrderId() != null){
                updateClaimDelayinsurance.setFlightOrderNum(claimDelayinsurance.getOrderId().toString());
            }

            if (claimDelayinsurance.getIsvalid() != null){
                updateClaimDelayinsurance.setClaimStart(claimDelayinsurance.getIsvalid().toString());
            }

            updateClaimDelayinsurance.setRemark(claimDelayinsurance.getRemark());

            List<PictureURLVo> pictureURLVoList = new ArrayList<>();
            List<ClaimDelayinsuranceImage> claimDelayinsuranceImages = claimDelayinsuranceImageMapper.claimDelayinsuranceImageList(claimDelayinsurance.getDelayinsuranceId());
            if (claimDelayinsuranceImages != null && claimDelayinsuranceImages.size() > 0){
                for (ClaimDelayinsuranceImage claimDelayinsuranceImage : claimDelayinsuranceImages) {
                    PictureURLVo pictureURLVo = new PictureURLVo();
                    pictureURLVo.setId(claimDelayinsuranceImage.getImageId());
                    pictureURLVo.setUrl(claimDelayinsuranceImage.getImageUrl());
                    pictureURLVo.setPictureType(claimDelayinsuranceImage.getImageType());
                    pictureURLVoList.add(pictureURLVo);
                }
            }
            updateClaimDelayinsurance.setPictureURLVoList(pictureURLVoList);
        }
        return updateClaimDelayinsurance;
    }

    /**
     * 修改保险理赔信息
     * */
    @Override
    public int upClaimDelayinsurance(UpdateClaimDelayinsurance updateClaimDelayinsurance) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ClaimDelayinsurance claimDelayinsurance = new ClaimDelayinsurance();
        claimDelayinsurance.setDelayinsuranceId(updateClaimDelayinsurance.getId());
        claimDelayinsurance.setFlightstatus(Long.valueOf(updateClaimDelayinsurance.getFlightStart()));

        if (updateClaimDelayinsurance.getVoyage() != null){
            String[] split = updateClaimDelayinsurance.getVoyage().split("-");
            String departure = split[0];
            String arrival = split[1];

            claimDelayinsurance.setDeparture(departure);
            CityVoApi departureCity = new CityVoApi();
            departureCity.setCityName(departure);
            departureCity = remoteTrainService.cityVo(departureCity);
            if (departureCity != null){
                claimDelayinsurance.setDeparturecode(departureCity.getCityCode());
            }
            else {
                throw new Exception("未查到出发城市三字码");
            }

            claimDelayinsurance.setArrival(arrival);
            CityVoApi arrivalCity = new CityVoApi();
            arrivalCity.setCityName(arrival);
            arrivalCity = remoteTrainService.cityVo(arrivalCity);
            if (arrivalCity != null){
                claimDelayinsurance.setArrivalcode(arrival);
            }
            else {
                throw new Exception("未查到到达城市三字码");
            }
        }

        try {
            claimDelayinsurance.setRealArriveDate(format.parse(updateClaimDelayinsurance.getActualFinishTime()));
            claimDelayinsurance.setRealFlyawayDate(format.parse(updateClaimDelayinsurance.getActualSetOffTime()));
            claimDelayinsurance.setArriveDate(format.parse(updateClaimDelayinsurance.getOriginalFinishTime()));
            claimDelayinsurance.setDepartureTime(format.parse(updateClaimDelayinsurance.getOriginalSetOffTime()));
            claimDelayinsurance.setDelaytime(format.parse(updateClaimDelayinsurance.getDelayTime()));
        } catch (Exception e){
            throw new Exception("日期转换错误");
        }
        claimDelayinsurance.setInsno(updateClaimDelayinsurance.getClaimPolicyNum());
        claimDelayinsurance.setIsvalid(Long.valueOf(updateClaimDelayinsurance.getClaimStart()));
        claimDelayinsurance.setFlightno(updateClaimDelayinsurance.getFlightNum());
        CustomerIdVo customerIdVo = new CustomerIdVo();
        customerIdVo.setCustomerName(updateClaimDelayinsurance.getCustomerName());
        customerIdVo.setCustomerPhone(updateClaimDelayinsurance.getPhone());
        customerIdVo = customerService.customerId(customerIdVo);
        if (customerIdVo != null){
            claimDelayinsurance.setCustomerId(customerIdVo.getCustomerId().toString());
        }
        else {
            throw new Exception("未查到会员id");
        }
        claimDelayinsurance.setOrderId(Long.valueOf(updateClaimDelayinsurance.getFlightOrderNum()));
        claimDelayinsurance.setRemark(updateClaimDelayinsurance.getRemark());
        int i = claimDelayinsuranceMapper.updateClaimDelayinsurance(claimDelayinsurance);
        if (i > 0){
            if (updateClaimDelayinsurance.getPictureURLVoList().size() > 0){
                for (PictureURLVo pictureURLVo : updateClaimDelayinsurance.getPictureURLVoList()) {
                    ClaimDelayinsuranceImage claimDelayinsuranceImage = new ClaimDelayinsuranceImage();
                    claimDelayinsuranceImage.setImageUrl(pictureURLVo.getUrl());
                    claimDelayinsuranceImage.setImageType(pictureURLVo.getPictureType());

                    int save;
                    //修改
                    if (pictureURLVo.getId() != null && pictureURLVo.getId() != 0){
                        claimDelayinsuranceImage.setImageId(pictureURLVo.getId());
                        save = claimDelayinsuranceImageMapper.updateClaimDelayinsuranceImage(claimDelayinsuranceImage);
                    }
                    //新增
                    else {
                        claimDelayinsuranceImage.setId(updateClaimDelayinsurance.getId());
                        save = claimDelayinsuranceImageMapper.insertClaimDelayinsuranceImage(claimDelayinsuranceImage);
                    }
                    if (save <= 0){
                        throw new Exception("保险图片保存失败" + pictureURLVo.getUrl());
                    }
                }
            }
            return 1;
        }
        else {
            return 0;
        }
    }

    /**
     * 延误险保障
     * */
    @Override
    public ClaimDelayinsuranceMaxVo claimDelayinsuranceMaxVo(HttpServletRequest request) throws Exception {

        Customer loginCustomer = loginService.getLoginCustomer(request);
        if (loginCustomer == null){
            throw new Exception("登录已过期");
        }

        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        ClaimDelayinsuranceMaxVo claimDelayinsuranceMaxVo = new ClaimDelayinsuranceMaxVo();
        ClaimDelayinsurance claimDelayinsurance = claimDelayinsuranceMapper.claimDelayinsuranceMax(loginCustomer.getCustomerId());
        if (claimDelayinsurance != null){
            claimDelayinsuranceMaxVo.setDelayInsuranceId(claimDelayinsurance.getDelayinsuranceId());
            String name = null;
            if (claimDelayinsurance.getFlightno() != null){
                String substring = claimDelayinsurance.getFlightno().substring(0, 2);
                Map<String, Object> map = new HashMap<>();
                map.put("card", substring);
                name = remoteFlightService.airlinerName(map);
            }

            if (name != null){
                claimDelayinsuranceMaxVo.setFlightInformation(name);
            }

            if (claimDelayinsurance.getFlightno() != null && claimDelayinsuranceMaxVo.getFlightInformation() != null){
                claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsuranceMaxVo.getFlightInformation() + claimDelayinsurance.getFlightno());
            }
            else if (claimDelayinsurance.getFlightno() != null){
                claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsurance.getFlightno());
            }

            String departureTime = null;
            if (claimDelayinsurance.getDepartureTime() != null){
                departureTime = format.format(claimDelayinsurance.getDepartureTime());
            }

            if (departureTime != null && claimDelayinsuranceMaxVo.getFlightInformation() != null){
                claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsuranceMaxVo.getFlightInformation() + departureTime);
            }
            else if (departureTime != null){
                claimDelayinsuranceMaxVo.setFlightInformation(departureTime);
            }

            if (claimDelayinsurance.getDeparturecode() != null){
                CityVoApi cityVoApi = new CityVoApi();
                cityVoApi.setCityCode(claimDelayinsurance.getDeparturecode());
                cityVoApi = remoteTrainService.cityVo(cityVoApi);
                if (cityVoApi != null){
                    claimDelayinsuranceMaxVo.setDepartureAirport(cityVoApi.getCityName());
                }
                else {
                    claimDelayinsuranceMaxVo.setDepartureAirport(claimDelayinsurance.getDeparturecode());
                }
            }

            if (claimDelayinsurance.getArrivalcode() != null){
                CityVoApi cityVoApi = new CityVoApi();
                cityVoApi.setCityCode(claimDelayinsurance.getArrivalcode());
                cityVoApi = remoteTrainService.cityVo(cityVoApi);
                if (cityVoApi != null){
                    claimDelayinsuranceMaxVo.setArriveAirport(cityVoApi.getCityName());
                }
                else {
                    claimDelayinsuranceMaxVo.setArriveAirport(claimDelayinsurance.getDeparturecode());
                }
            }

            if (claimDelayinsurance.getFlightstatus() != null){
                if (claimDelayinsurance.getFlightstatus() == 0){
                    claimDelayinsuranceMaxVo.setFlightStatus("航班延误");
                }
                else if (claimDelayinsurance.getFlightstatus() == 1){
                    claimDelayinsuranceMaxVo.setFlightStatus("航班取消");
                }
            }

            claimDelayinsuranceMaxVo.setPolicyNum(claimDelayinsurance.getInsno());
            if (claimDelayinsurance.getState() != null && claimDelayinsurance.getState() != 0){
                if (claimDelayinsurance.getState() == 0){
                    claimDelayinsuranceMaxVo.setInsuranceClaimStatus("初始");
                }
                else if (claimDelayinsurance.getState() == 1){
                    claimDelayinsuranceMaxVo.setInsuranceClaimStatus("理赔成功");
                }
                else if (claimDelayinsurance.getState() == 2){
                    claimDelayinsuranceMaxVo.setInsuranceClaimStatus("理赔失败");
                }
                else if (claimDelayinsurance.getState() == 3){
                    claimDelayinsuranceMaxVo.setInsuranceClaimStatus("安心打款失败");
                }
                else if (claimDelayinsurance.getState() == 4){
                    claimDelayinsuranceMaxVo.setInsuranceClaimStatus("安心打款成功");
                }
            }

            List<ClaimDelayinsuranceImage> claimDelayinsuranceImages = claimDelayinsuranceImageMapper.claimDelayinsuranceImageList(claimDelayinsurance.getDelayinsuranceId());
            if (claimDelayinsuranceImages.size() > 0){
                claimDelayinsuranceMaxVo.getClaimDelayinsuranceImages().addAll(claimDelayinsuranceImages);
//                String url = url(claimDelayinsuranceImages);
//                claimDelayinsuranceMaxVo.setUrl(url);
            }
        }
        return claimDelayinsuranceMaxVo;
    }

    /**
     * 延误险保障查询全部
     * */
    @Override
    public List<ClaimDelayinsuranceMaxVo> claimDelayinsuranceMaxList(HttpServletRequest request) throws Exception {
        Customer loginCustomer = loginService.getLoginCustomer(request);
        if (loginCustomer == null){
            throw new Exception("登录已过期");
        }
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        List<ClaimDelayinsuranceMaxVo> claimDelayinsuranceMaxVoList = new ArrayList<>();
        List<ClaimDelayinsurance> claimDelayinsuranceList = claimDelayinsuranceMapper.claimDelayinsuranceMaxList(loginCustomer.getCustomerId());
        if (claimDelayinsuranceList.size() > 0){
            for (ClaimDelayinsurance claimDelayinsurance : claimDelayinsuranceList) {
                ClaimDelayinsuranceMaxVo claimDelayinsuranceMaxVo = new ClaimDelayinsuranceMaxVo();

                if (claimDelayinsurance != null){
                    claimDelayinsuranceMaxVo.setDelayInsuranceId(claimDelayinsurance.getDelayinsuranceId());
                    String name = null;
                    if (claimDelayinsurance.getFlightno() != null){
                        String substring = claimDelayinsurance.getFlightno().substring(0, 2);
                        Map<String, Object> map = new HashMap<>();
                        map.put("card", substring);
                        name = remoteFlightService.airlinerName(map);
                    }

                    if (name != null){
                        claimDelayinsuranceMaxVo.setFlightInformation(name);
                    }

                    if (claimDelayinsurance.getFlightno() != null && claimDelayinsuranceMaxVo.getFlightInformation() != null){
                        claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsuranceMaxVo.getFlightInformation() + claimDelayinsurance.getFlightno());
                    }
                    else if (claimDelayinsurance.getFlightno() != null){
                        claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsurance.getFlightno());
                    }

                    String departureTime = null;
                    if (claimDelayinsurance.getDepartureTime() != null){
                        departureTime = format.format(claimDelayinsurance.getDepartureTime());
                    }

                    if (departureTime != null && claimDelayinsuranceMaxVo.getFlightInformation() != null){
                        claimDelayinsuranceMaxVo.setFlightInformation(claimDelayinsuranceMaxVo.getFlightInformation() + departureTime);
                    }
                    else if (departureTime != null){
                        claimDelayinsuranceMaxVo.setFlightInformation(departureTime);
                    }

                    if (claimDelayinsurance.getDeparturecode() != null){
                        CityVoApi cityVoApi = new CityVoApi();
                        cityVoApi.setCityCode(claimDelayinsurance.getDeparturecode());
                        cityVoApi = remoteTrainService.cityVo(cityVoApi);
                        if (cityVoApi != null){
                            claimDelayinsuranceMaxVo.setDepartureAirport(cityVoApi.getCityName());
                        }
                        else {
                            claimDelayinsuranceMaxVo.setDepartureAirport(claimDelayinsurance.getDeparturecode());
                        }
                    }

                    if (claimDelayinsurance.getArrivalcode() != null){
                        CityVoApi cityVoApi = new CityVoApi();
                        cityVoApi.setCityCode(claimDelayinsurance.getArrivalcode());
                        cityVoApi = remoteTrainService.cityVo(cityVoApi);
                        if (cityVoApi != null){
                            claimDelayinsuranceMaxVo.setArriveAirport(cityVoApi.getCityName());
                        }
                        else {
                            claimDelayinsuranceMaxVo.setArriveAirport(claimDelayinsurance.getDeparturecode());
                        }
                    }

                    if (claimDelayinsurance.getFlightstatus() != null){
                        if (claimDelayinsurance.getFlightstatus() == 0){
                            claimDelayinsuranceMaxVo.setFlightStatus("航班延误");
                        }
                        else if (claimDelayinsurance.getFlightstatus() == 1){
                            claimDelayinsuranceMaxVo.setFlightStatus("航班取消");
                        }
                    }

                    claimDelayinsuranceMaxVo.setPolicyNum(claimDelayinsurance.getInsno());
                    if (claimDelayinsurance.getState() != null && claimDelayinsurance.getState() != 0){
                        if (claimDelayinsurance.getState() == 0){
                            claimDelayinsuranceMaxVo.setInsuranceClaimStatus("初始");
                        }
                        else if (claimDelayinsurance.getState() == 1){
                            claimDelayinsuranceMaxVo.setInsuranceClaimStatus("理赔成功");
                        }
                        else if (claimDelayinsurance.getState() == 2){
                            claimDelayinsuranceMaxVo.setInsuranceClaimStatus("理赔失败");
                        }
                        else if (claimDelayinsurance.getState() == 3){
                            claimDelayinsuranceMaxVo.setInsuranceClaimStatus("安心打款失败");
                        }
                        else if (claimDelayinsurance.getState() == 4){
                            claimDelayinsuranceMaxVo.setInsuranceClaimStatus("安心打款成功");
                        }
                    }

                    List<ClaimDelayinsuranceImage> claimDelayinsuranceImages = claimDelayinsuranceImageMapper.claimDelayinsuranceImageList(claimDelayinsurance.getDelayinsuranceId());
                    if (claimDelayinsuranceImages.size() > 0){
                        claimDelayinsuranceMaxVo.getClaimDelayinsuranceImages().addAll(claimDelayinsuranceImages);
//                        String url = url(claimDelayinsuranceImages);
//                        claimDelayinsuranceMaxVo.setUrl(url);
                    }
                }
                claimDelayinsuranceMaxVoList.add(claimDelayinsuranceMaxVo);
            }
        }
        return claimDelayinsuranceMaxVoList;
    }

    /**
     * 保险理赔详情
     * */
    @Override
    public ClaimDetailsVo claimDetails(Long id) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        ClaimDetailsVo claimDetailsVo = new ClaimDetailsVo();
        ClaimDelayinsurance claimDelayinsurance = claimDelayinsuranceMapper.selectClaimDelayinsuranceById(id);
        if (claimDelayinsurance != null){
            CustomerIdVo customerIdVo = new CustomerIdVo();
            customerIdVo.setCustomerId(customerIdVo.getCustomerId());
            try {
                customerIdVo = customerService.customerId(customerIdVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }

            if (customerIdVo != null){
                claimDetailsVo.setPhone(customerIdVo.getCustomerPhone());
                claimDetailsVo.setCardNum(customerIdVo.getCustomerCard());
                claimDetailsVo.setName(customerIdVo.getCustomerName());
                claimDetailsVo.setCardType(customerIdVo.getCustomerCardType());
            }

            CaseSys caseSys = caseSysMapper.selectCaseSysById(claimDelayinsurance.getInvoiceId());
            if (caseSys != null){
                CaseType caseType = caseTypeMapper.selectCaseTypeById(caseSys.getCaseTypeId());
                if (caseType != null){
                    claimDetailsVo.setWorkOrderType(caseType.getCaseTypeName());
                }

                CaseSubType caseSubType = caseSubTypeMapper.selectCaseSubTypeById(caseSys.getCaseSubTypeId());
                if (caseSubType != null){
                    claimDetailsVo.setWorkOrder(caseSubType.getCaseSubType());
                }

                claimDetailsVo.setTitle(caseSys.getCaseSubject());
                claimDetailsVo.setClaimCount(caseSys.getClaimCount().toString());

                CaseClaim caseClaim = caseClaimMapper.selectCaseClaimById(caseSys.getCaseClaimId());
                if (caseClaim != null){
                    claimDetailsVo.setDuration(caseClaim.getDelaytime());
                    claimDetailsVo.setDate(format.format(caseClaim.getLatedate()));
                    if (caseClaim.getDepartureTime() != null){
                        claimDetailsVo.setOriginalDate(dateFormat.format(caseClaim.getDepartureTime()));
                    }
                    claimDetailsVo.setFlightNum(caseClaim.getFlightno());

                    if (claimDelayinsurance.getDeparturecode() != null){
                        CityVoApi cityVoApi = new CityVoApi();
                        cityVoApi.setCityCode(claimDelayinsurance.getDeparturecode());
                        cityVoApi = remoteTrainService.cityVo(cityVoApi);
                        if (cityVoApi != null){
                            claimDetailsVo.setSetOffAirport(cityVoApi.getCityName());
                        }
                        else {
                            claimDetailsVo.setSetOffAirport(caseClaim.getDeparturecode());
                        }
                    }

                    if (claimDelayinsurance.getArrivalcode() != null){
                        CityVoApi cityVoApi = new CityVoApi();
                        cityVoApi.setCityCode(claimDelayinsurance.getArrivalcode());
                        cityVoApi = remoteTrainService.cityVo(cityVoApi);
                        if (cityVoApi != null){
                            claimDetailsVo.setArriveAirport(cityVoApi.getCityName());
                        }
                        else {
                            claimDetailsVo.setArriveAirport(caseClaim.getDeparturecode());
                        }
                    }

                    claimDetailsVo.setJourneyForward(caseClaim.getDeparture());
                    claimDetailsVo.setJourneyBack(caseClaim.getArrival());
                    if (caseClaim.getFlightstatus() != null){
                        if (caseClaim.getFlightstatus().equals(EmHelper.workOrderType.Delay.getValue())){
                            claimDetailsVo.setFlightStart("航班延误");
                        }
                        else if (caseClaim.getFlightstatus().equals(EmHelper.workOrderType.Cancel.getValue())){
                            claimDetailsVo.setFlightStart("航班取消");
                        }
                    }

                    if (caseClaim.getDepartureTime() != null){
                        claimDetailsVo.setOriginalTakeOff(format.format(caseClaim.getDepartureTime()));
                    }

                    if (caseClaim.getRealFlyawayDate() != null){
                        claimDetailsVo.setActualTakeOff(format.format(caseClaim.getRealFlyawayDate()));
                    }

                    if (caseClaim.getScheduledlandingtime() != null){
                        claimDetailsVo.setOriginalLanding(format.format(caseClaim.getScheduledlandingtime()));
                    }

                    if (caseClaim.getReallandingtime() != null){
                        claimDetailsVo.setActualLanding(format.format(caseClaim.getReallandingtime()));
                    }

                    List<ClaimDelayinsuranceImage> claimDelayinsuranceImages = claimDelayinsuranceImageMapper.claimDelayinsuranceImageList(caseClaim.getCaseClaimId());
                    if (claimDelayinsuranceImages.size() > 0){
                        for (ClaimDelayinsuranceImage claimDelayinsuranceImage : claimDelayinsuranceImages) {
                            if (claimDelayinsuranceImage.getImageType() != null){
                                if (claimDelayinsuranceImage.getImageType().equals(EmHelper.OrderStatus.IDCardJust.getValue())){
                                    claimDetailsVo.setDocumentsFront(claimDelayinsuranceImage.getImageUrl());
                                }
                                else if (claimDelayinsuranceImage.getImageType().equals(EmHelper.OrderStatus.IDCardOpposite.getValue())){
                                    claimDetailsVo.setDocumentsReverse(claimDelayinsuranceImage.getImageUrl());
                                }
                                else if (claimDelayinsuranceImage.getImageType().equals(EmHelper.OrderStatus.BoardingPass.getValue())){
                                    claimDetailsVo.setBoardingPass(claimDelayinsuranceImage.getImageUrl());
                                }
                                else if (claimDelayinsuranceImage.getImageType().equals(EmHelper.OrderStatus.Prove.getValue())){
                                    claimDetailsVo.setFlightDelay(claimDelayinsuranceImage.getImageUrl());
                                }
                            }

                        }
                    }
                }

                claimDetailsVo.setAcceptingDepartment(caseSys.getReceivingDepartment());
                claimDetailsVo.setAcceptThePost(caseSys.getReceivePost());
                claimDetailsVo.setReceivingPersonnel(caseSys.getReceiver());
                if (caseSys.getPri() != null){
                    if (caseSys.getPri() == 0){
                        claimDetailsVo.setEmergencyLevel("普通");
                    }
                    else if (caseSys.getPri() == 1){
                        claimDetailsVo.setEmergencyLevel("紧急");
                    }
                }

                claimDetailsVo.setOrderType(caseSys.getOrderType());
                claimDetailsVo.setOrderNum(caseSys.getOrderId().toString());

                if (caseSys.getCaseRequireFinishTime() != null){
                    claimDetailsVo.setSpecifyTime(format.format(caseSys.getCaseRequireFinishTime()));
                }

                if (caseSys.getCaseExpFinishTime() != null){
                    claimDetailsVo.setExpectTime(format.format(caseSys.getCaseExpFinishTime()));
                }
            }
        }
        return claimDetailsVo;
    }

    /**
     * 新增保险理赔信息
     * */
    @Override
    public int insertInsuranceClaims(TmpInsuranceClaimsVo tmpInsuranceClaimsVo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ClaimDelayinsurance claimDelayinsurance = new ClaimDelayinsurance();
        claimDelayinsurance.setFlightno(tmpInsuranceClaimsVo.getFlightNumber());
        if (tmpInsuranceClaimsVo.getType().equals(EmHelper.workOrderType.Delay.getValue())){
            claimDelayinsurance.setFlightstatus(0l);
        }
        else if (tmpInsuranceClaimsVo.getType().equals(EmHelper.workOrderType.Cancel.getValue())){
            claimDelayinsurance.setFlightstatus(1l);
        }

        try {
            claimDelayinsurance.setRealFlyawayDate(format.parse(tmpInsuranceClaimsVo.getSetOffDate()));
        } catch (Exception e){
            throw new Exception("日期转换错误");
        }

        if (tmpInsuranceClaimsVo.getSetOffFlight() != null){
            claimDelayinsurance.setDeparturecode(tmpInsuranceClaimsVo.getSetOffFlight());
//            Map<String, Object> map = new HashMap<>();
//            map.put("airportId", tmpInsuranceClaimsVo.getSetOffFlight());
//            String airName = remoteFlightService.queryAirPortName(tmpInsuranceClaimsVo.getSetOffFlight());
//            AirportApi airportInfoByAirportName = remoteFlightService.getAirportInfoByAirportName(map);
//            if (airportInfoByAirportName != null){
//                claimDelayinsurance.setDeparturecode(airportInfoByAirportName.getAirportId());
//            }
        }

        if (tmpInsuranceClaimsVo.getEmdFlight() != null){
            claimDelayinsurance.setArrivalcode(tmpInsuranceClaimsVo.getEmdFlight());
//            Map<String, Object> map = new HashMap<>();
//            map.put("airportId", tmpInsuranceClaimsVo.getEmdFlight());
//            AirportApi airportInfoByAirportName = remoteFlightService.getAirportInfoByAirportName(map);
//            if (airportInfoByAirportName != null){
//                claimDelayinsurance.setArrivalcode(airportInfoByAirportName.getAirportId());
//            }
        }
        if (!StringHelper.isNullOrEmpty(tmpInsuranceClaimsVo.getOrderNum()))
        {
            claimDelayinsurance.setOrderId(Long.valueOf(tmpInsuranceClaimsVo.getOrderNum()));
        }
        claimDelayinsurance.setCustomerId(tmpInsuranceClaimsVo.getCustomerId());
        claimDelayinsurance.setCreateTime(DateTime.now());
        claimDelayinsurance.setState(0l);
        int i = claimDelayinsuranceMapper.insertClaimDelayinsurance(claimDelayinsurance);
        if (i > 0){
            if (tmpInsuranceClaimsVo.getClaimDelayinsuranceImageList().size() > 0){
                for (ClaimDelayinsuranceImage claimDelayinsuranceImage : tmpInsuranceClaimsVo.getClaimDelayinsuranceImageList()) {
                    claimDelayinsuranceImage.setId(claimDelayinsurance.getDelayinsuranceId());
                    int image = claimDelayinsuranceImageMapper.insertClaimDelayinsuranceImage(claimDelayinsuranceImage);
                    if (image <= 0){
                        throw new Exception("保险理赔图片添加失败" + claimDelayinsuranceImage.getImageUrl());
                    }
                }
            }
        }
        return 1;
    }

    /**
     * 获取登机牌图片url
     * */
    public String url(List<ClaimDelayinsuranceImage> claimDelayinsuranceImages){
        String url = null;
        int count = 0;
        String type;

        do{
            if (count == 0){
                type = EmHelper.OrderStatus.BoardingPass.getValue();
            }
            else if (count == 1){
                type = EmHelper.OrderStatus.Prove.getValue();
            }
            else if (count == 2){
                type = EmHelper.OrderStatus.IDCardJust.getValue();
            }
            else {
                type = EmHelper.OrderStatus.IDCardOpposite.getValue();
            }

            for (int i = 0 ; i < claimDelayinsuranceImages.size() ; i++) {
                if (claimDelayinsuranceImages.get(i).getImageType().equals(type)){
                    url = claimDelayinsuranceImages.get(i).getImageUrl();
                }
            }
        } while (url == null);

        return url;
    }
}
